package com.wushijia.classload;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class RememberingList implements InvocationHandler {

  private final List<String> proxies = new ArrayList<>();

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    switch (methodName) {
      case "clear":
        return null;
      case "remove":
        return false;
    }
    return method.invoke(proxies, args);
  }

  public static void main(String[] args) {
    RememberingList rememberingList = new RememberingList();
    List<String> list = (List<String>) Proxy.newProxyInstance(List.class.getClassLoader(), new Class[]{List.class}, rememberingList);
    list.add("cat");
    list.clear();
    System.out.println(list);
  }
}
